using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using System.Reflection;
using System.Diagnostics;

namespace CKS.FormsBasedAuthentication
{
    class MembershipReviewListFeatureReceiver : SPFeatureReceiver
    {
        #region Constants

        private string _ListName = "Site Membership Review List";
        private string _ListDesc = "Contains list of users requesting a site account.";
        //private static readonly string _AssemblyName = "SSN.EventHandlers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b6cf75d619319c66";
        //private static readonly string _ClassName = "SSN.EventHandlers.SSNMembershipReviewHandler";

        #endregion

        #region Methods

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPSite site = null;
            SPWeb web = null;
            Guid guidList;
            SPList list = null;

            try
            {
                site = properties.Feature.Parent as SPSite;
                web = site.RootWeb;

                if (web != null)
                {
                    guidList = web.Lists.Add(_ListName, _ListDesc, _ListName, new Guid("{69CE2076-9A2F-4c71-AEDF-F4252C01DE4E}").ToString(), (int)SPListTemplateType.GenericList, "100");
                    web.Update();

                    list = web.Lists[guidList];
                    list.EventReceivers.Add(SPEventReceiverType.ItemUpdated, Assembly.GetExecutingAssembly().FullName, "CKS.FormsBasedAuthentication.MembershipReviewHandler");
                    list.EventReceivers.Add(SPEventReceiverType.ItemAdded, Assembly.GetExecutingAssembly().FullName, "CKS.FormsBasedAuthentication.MembershipReviewHandler");
                    list.ContentTypesEnabled = false;
                    list.EnableAttachments = false;
                    list.EnableFolderCreation = false;
                    list.EnableVersioning = false;
                    list.NoCrawl = true;
      
                    list.Update();
                }
            }
            catch (Exception ex)
            {
                Utils.LogError(ex);
            }
            finally
            {
                if (web != null)
                    web.Dispose();
                if (site != null)
                    site.Dispose();
            }
        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPSite site = null;
            SPWeb web = null;
            SPList list;

            try
            {
                site = properties.Feature.Parent as SPSite;
                web = site.RootWeb;

                if (web != null)
                {
                    list = web.Lists[_ListName];
                    if (list != null)
                    {
                        list.Delete();
                        web.Update();
                    }
                }
            }
            catch (Exception ex)
            {
                Utils.LogError(ex);
            }
            finally
            {
                if (web != null)
                    web.Dispose();
                if (site != null)
                    site.Dispose();
            }   
        }

        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {

        }

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {

        }

        #endregion
    }
}
